﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using Shell32;

namespace com.his.mediaFileOrganizer
{
    class MediaProperties
    {
        string currentPath;
        
        string fileName;
        String size;
        DateTime createdOn;
        DateTime takenOn;

        public string Name
        {
            get
            {
                return fileName;
            }
        }

        public string Size
        {
            get { return size; }
        }

        public DateTime CreatedOn
        {
            get { return createdOn; }
        }

        public DateTime TakenOn
        {
            get { return takenOn; }
        }

        public string Path
        {
            get { return currentPath; }
        }

        public string TargetFolderPath
        {
            get
            {
                String targetPath = string.Empty;

                if (DateTime.MinValue != takenOn)
                {
                    targetPath = takenOn.Year + "\\" + takenOn.ToString("MMM");
                }
                else
                {
                    targetPath = createdOn.Year + "\\" + createdOn.ToString("MMM");
                }

                return targetPath;
            }
        }

        public MediaProperties(ref Folder folder, FolderItem2 item)
        {
            // 0, Name
            // 1, Size
            // 4, Date created
            // 12, Date taken
            // 179, Path
            // 176, Folder path

            fileName = folder.GetDetailsOf(item, 0);
            currentPath = folder.GetDetailsOf(item, 179);        
            size = folder.GetDetailsOf(item, 1);        
            DateTime.TryParse(folder.GetDetailsOf(item, 4), out createdOn);          
            String sDateTaken = folder.GetDetailsOf(item, 12);
            if ("" != sDateTaken)
            {
                string[] dateComponents = sDateTaken.Split(' ');
                string[] sDateParts = dateComponents[0].Split('/');
                string sDatePart = sDateParts[0].Remove(0, 1) + "/" + sDateParts[1].Remove(0, 1) + "/" + sDateParts[2].Remove(0, 1);
                string sTimePart = dateComponents[1].Remove(0, 2);

                takenOn = DateTime.Parse(sDatePart + " " + sTimePart);
            }
            
        }

        public System.Collections.Generic.List<String> getAllProperties()
        {

            System.Collections.Generic.List<string> arrHeaders = new System.Collections.Generic.List<string>();

            Shell32.Shell shell = new Shell32.Shell();
            Shell32.Folder objFolder;

            objFolder = shell.NameSpace(@"D:\Media\iPad\2013-09-26 ipad");

            for (int i = 0; i < short.MaxValue; i++)
            {
                string header = objFolder.GetDetailsOf(null, i);
                if (String.IsNullOrEmpty(header))
                    break;
                arrHeaders.Add(header);
            }

            foreach (Shell32.FolderItem2 item in objFolder.Items())
            {
                for (int i = 0; i < arrHeaders.Count; i++)
                {
                    Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(item, i));
                }
                Console.WriteLine("-----------------------------------------------------");
            }

            return arrHeaders;
        }
    }
}
